<?php
/**
 * Created by PhpStorm.
 * User: 17732
 * Date: 2018/6/20
 * Time: 13:24
 */

namespace app\admin\model;


use think\Db;
use think\Model;

class Books extends Model
{
    /**
     * 添加图书
     * @param array $arrBooks
     * @return int|string|void  success--1  fail--0
     */
    public static function addBooks($arrBooks=array()){
        if(empty($arrBooks) || !is_array($arrBooks)) return;
        $addbook = Db::name('books')->insert($arrBooks);
        return $addbook;
    }

    /**
     * 修改图书信息
     * @param array $arrBooks
     * @return int|string|void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function editBooks($arrBooks=array()){
        if(empty($arrBooks) || !is_array($arrBooks)) return;
        $editbook = Db::name('books')->update($arrBooks);
        return $editbook;
    }

    /**
     * 按图书名称进行图书查询
     */
    public static function selectBooks($bookName){
        $query = Db::name('books')
            ->where('bname',$bookName)
            ->where('isdelete','1')
            ->order('addtime','desc')
            ->find();
        return $query;
    }
    /**
     * 根据id查询出书籍
     */
    public static function selectOneBook($id){
        $query = Db::name('books')->where('id','in',$id)->select();
        return $query;
    }
    /**
     * 查询所有书籍数据
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function selectAllBooks(){
        $query = Db::name('books')
            ->order('operationtime','desc')
            ->select();
        return $query;
    }
    /**
     * 查询未删除的书籍
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function selectAllBooksNotDelete(){
        $query = Db::name('books')
            ->where('isdelete','1')
            ->select();
        return $query;
    }
    public static function selectAllBooksSearch($name){
        $query = Db::name('books')
            ->where('bname','like','%'.$name.'%')
//            ->where('isdelete','1')
            ->select();
        return $query;
    }

    /**
     * 查询出状态为 2 的图书
     */
    public static function statesEqOne(){
        $query = Db::name('books')
            ->where('bstates','in',1)
            ->where('isdelete','1')
            ->select();
        return $query;
    }
    /**
     * 查询出状态为 2 的图书 进行搜索
     */
    public static function statesEqOneSearch($name){
        $query = Db::name('books')
            ->where('bstates','in',1)
            ->where('isdelete','1')
            ->where('bname','like','%'.$name.'%')
            ->select();
        return $query;
    }
}